﻿using Newtonsoft.Json;
using SuperX.Common.Log;
using System;
using System.Text;
using Newtonsoft.Json.Linq;

namespace SuperX.SuperSocket.Socket.Client
{
    public class DataHelper
    {
        public static string ByteToHex(byte[] comByte, int len)
        {
            string returnStr = "";
            if (comByte != null)
            {
                for (int i = 0; i < len; i++)
                {
                    returnStr += comByte[i].ToString("X2") + " ";
                }
            }
            return returnStr;
        }

        public static byte[] HexToByte(string msg)
        {
            msg = msg.Replace(" ", "");

            byte[] comBuffer = new byte[msg.Length / 2];
            for (int i = 0; i < msg.Length; i += 2)
            {
                comBuffer[i / 2] = Convert.ToByte(msg.Substring(i, 2), 16);
            }

            return comBuffer;
        }

        public static string HextoAscii(string data)
        {
            data = data.Replace(" ", "");
            byte[] comBuffer = new byte[data.Length / 2];
            for (int i = 0; i < data.Length; i += 2)
            {
                comBuffer[i / 2] = Convert.ToByte(data.Substring(i, 2), 16);
            }
            string result = Encoding.Default.GetString(comBuffer);
            return result;
        }

        public static string AsciitoHex(string data)
        {
            StringBuilder result = new StringBuilder(data.Length * 2);
            for (int i = 0; i < data.Length; i++)
            {
                result.Append(((int)data[i]).ToString("X2") + " ");
            }
            return Convert.ToString(result);
        }

        /// <summary>
        /// 设备号进制转换：10进制转16进制
        /// </summary>
        /// <param name="dec"></param>
        /// <returns></returns>
        public static string DecToHex(int dec)
        {
            string result = dec.ToString("X2");
            return result;
            //while (result.Length < 4)
            //{
            //    result = "0" + result;
            //}
            //return result.Substring(0, 2) + " " + result.Substring(2, 2);
        }

        public static string DevHexToDec(string hex)
        {
            hex = hex.Replace(" ", "");
            int temp = 0;
            for (int i = 0; i < hex.Length; i++)
            {
                temp += Convert.ToInt32(hex.Substring(i, 1)) * Convert.ToInt32(Math.Pow(16, 3 - i));//0001
            }
            return temp.ToString();
        }

        public int HexToDec(string hex)
        {
            return Convert.ToInt32(hex, 16);
        }

        /// <summary>
        /// 转化bytes成16进制的字符
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public string BytesToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

        /// <summary>
        /// 高低对调
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string ReverseHexString(string str)
        {
            char[] buff = new char[str.Length];
            for (int i = 0; i < str.Length; i += 2)
            {
                buff[i] = str[str.Length - i - 2];
                buff[i + 1] = str[str.Length - 1 - i];
            }
            string s = new string(buff);
            return s;
        }

        public object DeserializeObject(string typeName, string content)
        {
            object reslut = null;
            try
            {
                var type = Type.GetType(typeName);//找相应的类型
                reslut = type != null ? JsonConvert.DeserializeObject(content, type) : JObject.Parse(content);
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);
            }
            return reslut;
        }
    }
}